/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.weed.net.impl;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Jason
 */
public class DefaultCookieHandler {

    
    protected Map<String, List<String>> cookiesMap = new HashMap<String, List<String>>();
    protected static DefaultCookieHandler cookiesHandler = new DefaultCookieHandler();
    List<String> cookies = null;
    
    
    private DefaultCookieHandler(){
        DefaultCookieStore dStore = new DefaultCookieStore();
                // 创建一个默认的 CookieManager
        CookieManager manager = new CookieManager(dStore,CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        // 将规则改掉，接受所有的 Cookie
       //manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); 
        // 保存这个定制的 CookieManager
        CookieHandler.setDefault(manager);
        
        
    }
    public static DefaultCookieHandler getInstance(){
        if(cookiesHandler ==null)
            cookiesHandler = new DefaultCookieHandler();
        return cookiesHandler;
    }
/**
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
        
        
        System.out.println("cookies=" + requestHeaders);
        return cookiesMap;
    }


    public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
        List<String> cookies =  responseHeaders.get("Set-Cookie");
        System.out.println("cookies=" + parseCookie(cookies.toString()));
        
    }
*/
    /**
     * 解析出cookies;
     * @param raw
     * @return
     */
     protected static String parseCookie(String raw) {
        String c = raw;
        if (raw != null) {
          int endIndex = raw.indexOf(";");
          if (endIndex >= 0) {
            c = raw.substring(0, endIndex);
          }
        }
        return c;
    }
}
